package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import com.android.kotlincoroutinemvvmdemo.R

/*
* WifiSignalShowView2
* */
class WifiSignalShowView2 @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : BaseView(context, attributeSet) {

    private var currentLevel = 4

    private val startAngle = 220F
    private val sweepAngle = 100F
    private val sweepAngle_2 = 360F

    private var mStrokeWidth = context.resources.getDimension(R.dimen.dp_2)

    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            color = context.resources.getColor(R.color.color_0C29E6)
            style = Paint.Style.FILL
        }
    }

    private val mPaint1 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeCap = Paint.Cap.ROUND
            color = context.resources.getColor(R.color.color_C70CE6)
            style = Paint.Style.STROKE
        }
    }

    private val mPaint1_0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeCap = Paint.Cap.ROUND
            color = context.resources.getColor(R.color.color_55C70CE6)
            style = Paint.Style.STROKE
        }
    }

    private val mPaint2 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeCap = Paint.Cap.ROUND
            color = context.resources.getColor(R.color.color_C70CE6)
            style = Paint.Style.FILL
        }
    }

    private val mPaint2_0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeCap = Paint.Cap.ROUND
            color = context.resources.getColor(R.color.color_55C70CE6)
            style = Paint.Style.FILL
        }
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        setBackgroundColor(context.resources.getColor(R.color.color_F3F3F3))
        mPaint1.strokeWidth = mStrokeWidth
        mPaint1_0.strokeWidth = mStrokeWidth
        val cx = mWidth / 2.0F
        val cy = mHeight / 2.0F
        canvas?.apply {
            drawCircle(
                cx,
                cy,
                cx,
                mPaint0
            )
            save()
            val space = mHeight.toFloat() / 30
            translate(0F,(mHeight - (mHeight.toFloat() - space * 13)) / 2)
            drawArc(
                space,
                space,
                mWidth.toFloat() - space,
                mHeight.toFloat() - space,
                startAngle,
                sweepAngle,
                false,
                if (currentLevel > LEVEL_3) mPaint1 else mPaint1_0
            )

            drawArc(
                space * 5,
                space * 5,
                mWidth.toFloat() - space * 5,
                mHeight.toFloat() - space * 5,
                startAngle,
                sweepAngle,
                false,
                if (currentLevel > LEVEL_2) mPaint1 else mPaint1_0
            )

            drawArc(
                space * 9,
                space * 9,
                mWidth.toFloat() - space * 9,
                mHeight.toFloat() - space * 9,
                startAngle,
                sweepAngle,
                false,
                if (currentLevel > LEVEL_1) mPaint1 else mPaint1_0
            )

            drawArc(
                space * 13,
                space * 13,
                mWidth.toFloat() - space * 13,
                mHeight.toFloat() - space * 13,
                startAngle,
                sweepAngle_2,
                false,
                if (currentLevel < LEVEL_1) mPaint2_0 else mPaint2
            )
            restore()
        }
    }

    companion object {
        const val LEVEL_4 = 4
        const val LEVEL_3 = 3
        const val LEVEL_2 = 2
        const val LEVEL_1 = 1
    }
}